class TreeNode:
  def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None


class Solution:
  def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
    if not A or not B: return False
    queue = [A]
    while len(queue) > 0:
      node = queue.pop(0)
      if node.val == B.val and self.compare(node, B):
        return True
      if node.left:
        queue.append(node.left)
      if node.right:
        queue.append(node.right)
    return False
  
  def compare(self, A: TreeNode, B: TreeNode) -> bool:
    if not B:
      return True
    if not A or A.val != B.val:
      return False
    return self.compare(A.left, B.left) and self.compare(A.right, B.right)


def stringToTreeNode(input):
  input = input.strip()
  input = input[1:-1]
  if not input:
    return None

  inputValues = [s.strip() for s in input.split(',')]
  root = TreeNode(int(inputValues[0]))
  nodeQueue = [root]
  front = 0
  index = 1
  while index < len(inputValues):
    node = nodeQueue[front]
    front = front + 1

    item = inputValues[index]
    index = index + 1
    if item != "null":
      leftNumber = int(item)
      node.left = TreeNode(leftNumber)
      nodeQueue.append(node.left)

    if index >= len(inputValues):
      break

    item = inputValues[index]
    index = index + 1
    if item != "null":
      rightNumber = int(item)
      node.right = TreeNode(rightNumber)
      nodeQueue.append(node.right)
  return root


def main():
  import sys
  import io

  def readlines():
    for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
      yield line.strip('\n')

  lines = readlines()
  while True:
    try:
      line = next(lines)
      A = stringToTreeNode(line)
      line = next(lines)
      B = stringToTreeNode(line)

      ret = Solution().isSubStructure(A, B)

      out = (ret)
      print(out)
    except StopIteration:
      break


'''
STATUS: LEARNT
TRIES:  N/A
TIME:   128 MS
MEMORY: 18 MB
'''
if __name__ == '__main__':
  main()
